package com.fy56.platform.goodsTracking.service.impl;

import com.dexcoder.dal.JdbcDao;
import com.dexcoder.dal.build.Criteria;
import com.fy56.platform.goodsTracking.model.CollectiongoodsvalueaccountPublic;
import com.fy56.platform.goodsTracking.model.GoodstrackedRecordTopPublic;
import com.fy56.platform.goodsTracking.model.GoodstrackedRecordTopPublicF6;
import com.fy56.platform.goodsTracking.model.TransportbillmasterPublic;
import com.fy56.platform.goodsTracking.service.GoodstrackedRecordTopPublicService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by XTK on 2016-11-21.
 */
@Repository
public class GoodstrackedRecordTopPublicImpl implements GoodstrackedRecordTopPublicService {
    static Logger log = Logger.getLogger(GoodstrackedRecordTopPublicService.class);
    @Autowired
    JdbcDao jdbcDao_GoodsTracked;

    @Override
    public List<GoodstrackedRecordTopPublic> selectByOrganizationcode(String organizationcode) {
        Criteria criteria = Criteria.select(GoodstrackedRecordTopPublic.class)
                .where("organizationcode", new Object[]{organizationcode});
        return jdbcDao_GoodsTracked.queryList(criteria);
    }

    @Override
    public List<GoodstrackedRecordTopPublic> selectByNotUpload(String organizationcode, String beginTime, String endTime) {
        Criteria criteria = Criteria.select(GoodstrackedRecordTopPublic.class)
                .where("organizationcode", new Object[]{organizationcode})
                .and("Systime", ">=", new Object[]{beginTime})
                .and("Systime", "<=", new Object[]{endTime})
                .and("upload", new Object[]{null})
                .asc("systime");
        return jdbcDao_GoodsTracked.queryList(criteria);
    }

    @Override
    public List<GoodstrackedRecordTopPublic> selectByNotUpload_Police(String organizationcode, String beginTime, String endTime) {
        Criteria criteria = Criteria.select(GoodstrackedRecordTopPublic.class)
                .where("organizationcode", new Object[]{organizationcode})
                .and("Systime", ">=", new Object[]{beginTime})
                .and("Systime", "<=", new Object[]{endTime})
                .asc("systime");
        return jdbcDao_GoodsTracked.queryList(criteria);
    }

    @Override
    public List<GoodstrackedRecordTopPublicF6> selectNotList(String organizationcode, String beginTime, String endTime) {
//        Criteria criteria = Criteria.select(GoodstrackedRecordTopPublic.class)
//                .where("Systime", ">=", new Object[]{beginTime})
//                .and("Systime", "<=", new Object[]{endTime})
//                .and("Traceplatformid",new Object[]{" is null "})
//                .asc("systime");
//        return jdbcDao_GoodsTracked.queryList(criteria);
        String sql = "select Convert(decimal(18,2),totalnumberofpackages) as totalnumberofpackages," +
                "CONVERT(varchar(100), GoodsTracked_Record_Top_Public.SysTime, 20) SYSTIME " +
                ",GoodsTracked_Record_Top_Public.ConsignmentBillNumber " +
                ",GoodsTracked_Record_Top_Public.GoodsNumber " +
                ",GoodsTracked_Record_Top_Public.ManualNumber,GoodsTracked_Record_Top_Public.* " +
                "from GoodsTracked_Record_Top_Public WITH(NOLOCK)  " +
                " where  1=1 " +
                " and GoodsTracked_Record_Top_Public.Systime>='" + beginTime + "'" +
                " and GoodsTracked_Record_Top_Public.Systime<='" + endTime + "'" +
//                " and GoodsTracked_Record_Top_Public.organizationcode='fycsz'" +
                " and (GoodsTracked_Record_Top_Public.uploadFlag is null or GoodsTracked_Record_Top_Public.uploadFlag='') " +
                " order by GoodsTracked_Record_Top_Public.systime asc ";
//        System.out.println("selNotUploadList:"+sql);
        List<GoodstrackedRecordTopPublicF6> result = null;
        try{
            result = jdbcDao_GoodsTracked.queryListForSql(sql, GoodstrackedRecordTopPublicF6.class);
        }catch (Exception e){
            log.error("selectNotList error:",e);
        }
        return result;
    }

    @Override
    public void updateByupload(GoodstrackedRecordTopPublic top) {
        jdbcDao_GoodsTracked.updateForSql("update GoodsTracked_Record_Top_Public " +
                "set upload='" + top.getUpload()
                + "',uploadError='"
                + top.getUploaderror() + "' where id=" + top.getId());
    }

    @Override
    public void updateTyCallback(String consignmentbillmasterid,String code,String message){
        jdbcDao_GoodsTracked.updateForSql("update GoodsTracked_Record_Top_Public " +
                "set tyCallbackCode='" + code
                + "',tyCallbackMessage='"
                + message + "' where consignmentbillmasterid='" + consignmentbillmasterid+"'");
    }

    @Override
    public List<TransportbillmasterPublic> selectCarNotUpload(String organizationcode, String beginTime, String endTime) {
//        Criteria criteria = Criteria.select(GoodstrackedRecordTopPublic.class)
//                .where("Systime", ">=", new Object[]{beginTime})
//                .and("Systime", "<=", new Object[]{endTime})
//                .and("Traceplatformid",new Object[]{" is null "})
//                .asc("systime");
//        return jdbcDao_GoodsTracked.queryList(criteria);
        String sql = "select t.*  " +
                "from TransportBillMaster_public t WITH(NOLOCK)  " +
                " where  1=1 " +
                " and t.systime>='" + beginTime + "'" +
                " and t.systime<='" + endTime + "'" +
//                " and GoodsTracked_Record_Top_Public.organizationcode='" + organizationcode + "'" +
                " and (t.uploadstate is null or t.uploadstate='') " +
                " order by t.systime asc ";
//        System.out.println("selNotUploadList:"+sql);
        List<TransportbillmasterPublic> result = null;
        try{
            result = jdbcDao_GoodsTracked.queryListForSql(sql, TransportbillmasterPublic.class);
        }catch (Exception e){
            log.error("selectNotList error:",e);
        }
        return result;
    }



    @Override
    public List<CollectiongoodsvalueaccountPublic> selectCollectionMoneyNotUpload(String organizationcode, String beginTime, String endTime) {
//        Criteria criteria = Criteria.select(GoodstrackedRecordTopPublic.class)
//                .where("Systime", ">=", new Object[]{beginTime})
//                .and("Systime", "<=", new Object[]{endTime})
//                .and("Traceplatformid",new Object[]{" is null "})
//                .asc("systime");
//        return jdbcDao_GoodsTracked.queryList(criteria);
        String sql = "select t.*  " +
                "from CollectionGoodsValueAccount_Public t WITH(NOLOCK)  " +
                " where  1=1 " +
                " and t.systime>='" + beginTime + "'" +
                " and t.systime<='" + endTime + "'" +
//                " and GoodsTracked_Record_Top_Public.organizationcode='" + organizationcode + "'" +
                " and (t.flag is null or t.flag='')  " +
                " order by t.systime asc ";
//        System.out.println("selNotUploadList:"+sql);
        List<CollectiongoodsvalueaccountPublic> result = null;
        try{
            result = jdbcDao_GoodsTracked.queryListForSql(sql, CollectiongoodsvalueaccountPublic.class);
        }catch (Exception e){
            log.error("selectNotList error:",e);
        }
        return result;
    }
    @Override
    public List<TransportbillmasterPublic> selectCarNotUploadBC(String organizationcode, String beginTime, String endTime,int num) {
//        Criteria criteria = Criteria.select(GoodstrackedRecordTopPublic.class)
//                .where("Systime", ">=", new Object[]{beginTime})
//                .and("Systime", "<=", new Object[]{endTime})
//                .and("Traceplatformid",new Object[]{" is null "})
//                .asc("systime");
//        return jdbcDao_GoodsTracked.queryList(criteria);
        String sql = "select top "+num+" TransportBillMasterId, t.*  " +
                "from TransportBillMaster_public t WITH(NOLOCK)  " +
                " where  1=1 " +
                " and t.systime>='" + beginTime + "'" +
                " and t.systime<='" + endTime + "'" +
//                " and GoodsTracked_Record_Top_Public.organizationcode='" + organizationcode + "'" +
                " and (t.uploadstate is null or t.uploadstate='') " +
                " order by t.systime asc ";
//        System.out.println("selNotUploadList:"+sql);
        List<TransportbillmasterPublic> result = null;
        try{
            result = jdbcDao_GoodsTracked.queryListForSql(sql, TransportbillmasterPublic.class);
        }catch (Exception e){
            log.error("selectNotList error:",e);
        }
        return result;
    }
    @Override
    public List<CollectiongoodsvalueaccountPublic> selectCollectionMoneyNotUploadBC(String organizationcode, String beginTime, String endTime,int num) {
        String sql = "select top "+num+" CollectionGoodsValueAccountId t.*  " +
                "from CollectionGoodsValueAccount_Public t WITH(NOLOCK)  " +
                " where  1=1 " +
                " and t.systime>='" + beginTime + "'" +
                " and t.systime<='" + endTime + "'" +
                " and (t.flag is null or t.flag='' or t.flag='2') " +
                " order by t.systime asc ";
        List<CollectiongoodsvalueaccountPublic> result = null;
        try{
            result = jdbcDao_GoodsTracked.queryListForSql(sql, CollectiongoodsvalueaccountPublic.class);
        }catch (Exception e){
            log.error("selectNotList error:",e);
        }
        return result;
    }
}
